<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%String path = request.getContextPath();%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>

<script type="text/javascript" src="<%=path %>/resource/lib/plugins/ajaxfileupload.js"></script>

<div class="fieldset1 tabel_col tabel_col_s clearFix">
    <div class="caption">
        <span class="title">附件信息</span>
    </div>
    <div class="fieldBox col_100">
        <div class="sqPanel">
            <div id="stlFileDataGrid" class="sq-grid"></div>
        </div>
    </div>
</div>

<script type="text/javascript">
    var basePath = '<%=path%>';
    var _pkId = '<%=request.getParameter("pkId") %>';
    var _filePath = '<%=request.getParameter("filePath") %>';
    var _optAuth = '<%=request.getParameter("optAuth") %>';
    var fileGrid;

    $(function () {
        initFileList();

    });

    function initFileList() {
        var hideFlag = true;
        var auth = "";
        if (_optAuth == '1') {
            hideFlag = false;
            auth = "10"
        } else if (_optAuth == '2') {
            hideFlag = false;
            auth = "20,30,40";
        } else if (_optAuth == '3') {
            hideFlag = false;
            auth = "10,20,30,40";
        }
        fileGrid = $("#stlFileDataGrid").ligerGrid({
            url: basePath + '/settlement/file/loadList.do?pkId=' + _pkId ,
            method: 'get',
            columns: [
                {display: '', name: 'upload', width: 80, align: 'center', enabledSort: false, hide: hideFlag,
                    render: function (rowdata, rowindex, value) {
                        var h = '';
                        if (auth.indexOf(rowdata.cfgDtlCd) != -1) {
                            h += '<span class="fileupload-button sq-button-hasicon"><i class="sqBtnIco_9"></i>上传<input type="file" id="fileup_' + rowdata.cfgCd + '_' + rowdata.cfgDtlCd + '" multiple name="fileup_' + rowdata.cfgCd + '_' + rowdata.cfgDtlCd + '"' +
                                ' class="fileupload-input" onchange="onChangefile_multi(event,\'' + rowdata.cfgCd + '\',\'' + rowdata.cfgDtlCd + '\',\'' + rowdata.orgCd + '\',1)"></span>';
                        }
                        return h;
                    }
                },
                {display: '资料名称', name: 'datNm', align: 'center', minWidth: 200, enabledSort: false},
                {display: '上传文件名称 <span class="sq-button-hasicon" title="一键下载" onclick="downloadByUrl()"><i class="sqBtnIco_8"></i></span></div>', name: 'fileNm', minWidth: 280, enabledSort: false,
                    render: function (rowdata, rowindex, value) {
                        if (value) {
                            var h;
                            h = '<a href="javascript:uploadFileByUrl(';
                            h += "'" + rowdata.filePath + "'";
                            h += ",'" + rowdata.fileNm + "'";
                            h += ')">' + rowdata.fileNm + '</a>'
                            return h;
                        }
                    }
                },
                {display: '上传时间', name: 'crtTm',align: 'center', minWidth: 180, enabledSort: false, render:formatDate},
                /*{display: '状态', name: 'audStsCdNm', minWidth: 100, enabledSort: false},*/
                {display: '操作', name: 'operate', minWidth: 100, align: 'center', enabledSort: false, hide: hideFlag,
                    render: function (rowdata, rowindex, value) {
                        var h = '';
                        if (rowdata.fileId && auth.indexOf(rowdata.cfgDtlCd) != -1) {
                            h += '<a class="sq-button-hasicon" style="cursor:pointer;" onclick="delFile(event,' + rowindex + ');"><i class="sqBtnIco_7"></i>删除</a>';
                        }
                        return h;
                    }
                }
            ],
            root: 'rows',
            hideLoadButton: true,
            width: '100%',
            height: '200px',
            isScroll: false,
            rownumbers: true,  //显示序号
            checkbox: false,
            allowUnSelectRow: true,
            alternatingRow: false,
            frozen: false,
            usePager: false,
            onAfterShowData: function () {
                $('#stlFileDataGrid').rowspan('datNm');
                $('#stlFileDataGrid').rowspan('upload');
            },
        });
    }

    //删除文件
    function delFile(event, rowindex){
        var rowdata = fileGrid.getRow(rowindex);
        $.ligerDialog.confirm("确认删除  " + rowdata.fileNm + "  吗?", function (flag) {
            if (!flag) {
                return;
            }else{
                var wait_dialog;
                $.ajax({
                    type: 'post',
                    dataType: 'json',
                    data: {'fileId': rowdata.fileId},
                    url: basePath + '/settlement/file/removeFile.do',
                    beforeSend: function () {
                        wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
                    },
                    complete: function () {
                        wait_dialog.close();
                    },
                    success: function (msg) {
                        if (msg.success == 1) {
                            $.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                            fileGrid.reload();
                        }else{
                            $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                        }
                    }
                });
            }
        });
    }

    function onChangefile_multi(e,cfgCd,cfgDtlCd,orgCd) {
        ajaxFileUploadImg(e.target.id, function(fileData){
            var data = {
                'fileParams': JSON.stringify(fileData),
                'pkId': _pkId,
                'cfgCd': cfgCd,
                'cfgDtlCd': cfgDtlCd,
                'orgCd': orgCd
            };
            AjaxUtils.ajaxPost(basePath + "/settlement/file/saveFile.do", data, function (msg) {
                if (msg.success == 1) {
                    fileGrid.reload();
                } else {
                    UIUtils.alertError(msg.info);
                }
            }, true);
        });
    }

    //上传方法
    function ajaxFileUploadImg(fileId, callback) {
        if ($("#" + fileId).val() == "") {
            $.ligerDialog.SQalert({content: "请选择要上传的附件！", SQ_type: "warning"});
            return false;
        }
        var wait_dialog = $.ligerDialog.SQalert({content: '上传中,请稍后...', SQ_type: "waitting"});
        //文件上传
        $.ajaxFileUpload({
            url: basePath + '/system/sysulfilesrec/upload.do',
            secureuri: false,
            fileElementId: fileId,  //上传框的id
            dataType: 'json',
            data: {filePath: _filePath},
            success: function (msg) {
                if (msg.success == 1) {
                    callback(msg.data);
                } else {
                    $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                }
            },
            complete: function () {
                wait_dialog.close();
            }
        });
    }

    function uploadFileByUrl(_url, _name) {
        if (SQ.regs.img.test(_name)) {
            //SQ.imgPreview(_url);
            //SQ.imgPreview(_url, true);
            window.__ImgSrc = _url;
            window.__ImgTit = _name;
            window.open('<%=path%>/project/prjbscinfo/imgWinOpen.do', '', 'location=yes, width=800, height=600');
        } else if (SQ.regs.img.test(_url)) {
            window.__ImgSrc = _url;
            window.__ImgTit = _name;
            window.open('<%=path%>/project/prjbscinfo/imgWinOpen.do', '', 'location=yes, width=800, height=600');
        } else {
            window.open(_url);
        }
    }

    function downloadByUrl() {
        var data = fileGrid.getData();
        var ids = [];
        for (var i = 0; i < data.length; i++) {
            if(StringUtils.isNotEmpty(data[i].fileId)){
                ids.push(data[i].fileId);
            }
        }
        if (ids.length == 0) {
            $.ligerDialog.SQalert({content: "没有可下载的文件！", SQ_type: "warning"});
            return;
        }
        var actoinURL = basePath + '/system/sysulfilesrec/zipDownloadById.do?';
        var form = $("<form>");
        $('body').append(form);
        form.attr('style','display:none');
        form.attr('target','');
        form.attr('method','post');
        form.attr('action',actoinURL);//下载文件的请求路径
        var input = $('<input>');
        input.attr('type','hidden');
        input.attr('name','ids');
        input.attr('value',JSON.stringify(ids));
        form.append(input);
        form.submit();
    }

    function formatDate(rowdata,index,value){
        var dateStr = "";
        if(null != value && value != '' && '长期' != value ){
            var date = new Date(value);
            dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DDHHMMSS);
        }
        return dateStr;
    }


</script>